﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SessionHelper.cs" company="Tuckerman Software">
// Copyright 2012 Tuckerman Software, New Hampshire, USA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Circles.NHibernateRepository
{
    using NHibernate;

    /// <summary>
    /// Helper methods that make working with NHibernate Session easier
    /// </summary>
    public static class SessionHelper
    {
        /// <summary>
        /// Delete an Entity instance using an Id.
        /// </summary>
        /// <typeparam name="TEntity">Type of entity</typeparam>
        /// <param name="session">The current <see cref="ISession"/></param>
        /// <param name="id">Unique identifier of entity to delete.</param>
        /// <remarks>
        /// Credit to Diego Mijelshon: http://stackoverflow.com/a/3065173
        /// </remarks>
        public static void Delete<TEntity>(this ISession session, object id)
        {
            var queryString = string.Format("delete {0} where id = :id", typeof(TEntity));
            session.CreateQuery(queryString)
                   .SetParameter("id", id)
                   .ExecuteUpdate();
        }
    }
}
